/**
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *	
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.brightcove.android.oneplanet;

import java.util.logging.Logger;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.brightcove.mobile.android.BCPlayerView;
import com.brightcove.mobile.mediaapi.ReadAPI;
import com.brightcove.mobile.mediaapi.model.Video;

/**
 * This Activity manages the Brightcove Player
 */
public class Player extends Activity {
	private static final String TAG = "Player";
	
	public static final String VIDEO_ID = "videoId";
	
	private BCPlayerView player;
	
	/** 
     * Called when the activity is first created. 
     */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.player);

    	player = (BCPlayerView) findViewById(R.id.player);
		
		playVideo();
	}
	
    /**
     * We specify this callback because this activity is configured to respond to orientation
     * changes so that they can be tested.
     * 
     * @see android.app.Activity#onConfigurationChanged(android.content.res.Configuration)
     */
    @Override
    public void onConfigurationChanged(Configuration pNewConfig) {
        super.onConfigurationChanged(pNewConfig);
        Log.v(TAG, "onConfigurationChanged()");

        Log.d(TAG, "Player isPlaying? " + player.isPlaying());
    }
	
    /**
     * Makes a findVideoById API call and then tells the BC Player
     * to play the result.
     */
    private void playVideo() {
    	
        // Make sure we have a player.
        if (player == null) {
            Log.e("BCPlaybackTester.playVideo", "No player found.  Giving up.");
            return;
        }

        // Read the extra data from the Intent.
        Intent intent = getIntent();

        Long videoId = intent.getLongExtra(VIDEO_ID, Long.MAX_VALUE);

        // Setup the Read API.
        Logger log = Logger.getLogger("BCAndroidAPILogger"); // create BC API wrapper
        ReadAPI readAPI = new ReadAPI(APIToken.API_TOKEN);
        readAPI.setLogger(log);

        try {
            Video video = readAPI.findVideoById(videoId, null, null);

            if (video != null) {
                player.load(video);
                player.start();
            } else {
                Log.e("BCPlaybackTester.playVideo", "No video found for id: " + videoId);
            }
        } catch (Exception e) {
            Log.e(TAG, "error in temp code", e);
        }
    }
}
